<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<body>
	<script type="text/javascript">
		$(function() {
			$("#role_datagrid").datagrid({
				width : 'auto',
				height : 'auto',
				fit:true,
				pageSize : 20,
				pageList : [ 5, 10, 20 ],
				nowrap : false,
				striped : true,
				//singleSelect : true,
				url : "role/datagrid.action",
				loadMsg : "数据正在加载……",
				pagination : true,
				rownumbers : true,
				columns : [ [ 
				    {field:"ck",checkbox:true} ,      
					{field : 'name',title : '角色名',align : 'center',width : '100'},
					{field : 'remark',title : '备注',align : 'center',width : '100'},
					{field : 'createDate',title : '创建时间',align : 'center',width : '100'}
				] ],
				toolbar:[{
					text:"新增",
					handler:function() {
						$("#role_add_dialog").dialog({
							title:"新增角色",
							width: 300,
						    height: 200,
						    closed: false,
						    cache: false,
						    href: 'platform/role/add.jsp',
						    modal: true,
						    buttons:[{
								text:'保存',
								handler:function(){
									var data = $("#role_form").serialize();
									$.ajax({
										url:"role/add.action",
										type:"post",
										data:data,
										dataType:"json",
										success:function(data) {
											$.messager.alert("提示", data.msg, "info", function() {
												if(data.isSuccess) {
													$("#role_add_dialog").dialog("close");
													$("#role_datagrid").datagrid("load");
												}
											});
										}
									});
								}
							},{
								text:'关闭',
								handler:function(){
									$("#role_add_dialog").dialog("close");
								}
							}]
						});
					}
				},'-',{
					text:"删除",
					handler:function() {
						var checks = $("#role_datagrid").datagrid("getChecked");
						var ids = "";
						var len = checks.length;
						if(len==0) $.messager.alert("提示", "请选择要删除的记录！！");
						else {
							$.each(checks, function(i, n){
								ids+=n.id+(i<len-1?",":"");
							}); 
							$.ajax({
								url:"role/del.action",
								type:"post",
								data:{ids:ids},
								dataType:"json",
								success:function(data) {
									$.messager.alert("提示", data.msg, "info", function() {
										if(data.isSuccess) {
											$("#role_datagrid").datagrid("load");
										}
									});
								}
							});
						}
					}
				},'-',{
					text:"分配菜单",
					handler:function() {
						var checks = $("#role_datagrid").datagrid("getChecked");
						var len = checks.length;
						if(len==0) $.messager.alert("提示", "请选择分配菜单的角色！！");
						else if(len>1) $.messager.alert("提示", "一次只能给一个角色分配菜单！！");
						else {
							var check_role_id = checks[0].id;
							$("#menu_tree_dialog").dialog({
								title:"菜单授权",
								width: 300,
							    height: 600,
							    closed: false,
							    cache: false,
							    href: 'platform/role/menu.jsp?id='+check_role_id,
							    modal: true,
							    buttons:[{
							    	text:"授权",
							    	handler:function() {
							    		var checks = $("#role_menu_tree").tree("getChecked");
							    		var len = checks.length;
							    		var ids = "";
							    		$.each(checks, function(i, n){
							    			ids+=n.id+(i<len-1?",":"");
							    		});
							    		$.ajax({
							    			url:"role/menu_save.action",
							    			type:"post",
							    			data:{
							    				menuIds:ids,
							    				roleId:check_role_id
							    			},
							    			dataType:"json",
							    			success:function(data) {
							    				$.messager.alert("提示", data.msg, "info", function() {
							    					if(data.isSuccess) $("#menu_tree_dialog").dialog("close");
							    				});
							    			}
							    		});
							    	}
							    },{
							    	text:"关闭",
							    	handler:function() {
							    		$("#menu_tree_dialog").dialog("close");
							    	}
							    }]
							});
							
						}
					}
				},'-',{
					text:"分配用户",
					handler:function() {
						var checks = $("#role_datagrid").datagrid("getChecked");
						var len = checks.length;
						if(len==0) $.messager.alert("提示", "请选择分配用户的角色！！");
						else if(len>1) $.messager.alert("提示", "一次只能给一个角色分配用户！！");
						else {
							$("#role_user_dialog").dialog({
								title:"机构-用户列表",
								width: 850,
							    height: 470,
							    closed: false,
							    cache: false,
							    content:"<iframe id='role_user_iframe' src='platform/role/user.jsp'"+
							    " style='width:800px;height:390px;'></iframe>",
							    modal: true,
							    //resizable:true,
							    buttons:[{
							    	text:"分配",
							    	handler:function() {
							    		//console.info($(this));
							    		var content = $("#role_user_iframe").contents().find("#content");
							    		var ids = content.find("span").map(function(){
							    			  return $(this).attr("id");
							    		}).get().join(",");
							    		$.ajax({
							    			url:"role/user_save.action",
							    			type:"post",
							    			data:{roleId:checks[0].id, userIds:ids},
							    			dataType:"json",
							    			success:function(data) {
							    				$.messager.alert("提示", data.msg, "info", function() {
							    					if(data.isSuccess) $("#role_user_dialog").dialog("close");
							    				});
							    			}
							    		});
							    	}
							    },{
							    	text:"关闭",
							    	handler:function() {
							    		$("#role_user_dialog").dialog("close");
							    	}
							    }]
							});
						}
					}
				}]
			});
		});
		
	</script>
	<table id="role_datagrid"></table>
	<div id="role_add_dialog"></div>
	<div id="menu_tree_dialog"></div>
	<div id="role_user_dialog"></div>
</body>